package cn.stylefeng.guns.modular.shoppingcart.controller;

import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.merchandise.service.IMerService;
import cn.stylefeng.guns.modular.order.service.IOrderMerService;
import cn.stylefeng.guns.modular.order.service.IOrderService;
import cn.stylefeng.guns.modular.order.service.impl.OrderServiceImpl;
import cn.stylefeng.guns.modular.system.model.Mer;
import cn.stylefeng.guns.modular.system.model.Order;
import cn.stylefeng.guns.modular.system.model.OrderMer;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Shoppingcart;
import cn.stylefeng.guns.modular.shoppingcart.service.IShoppingcartService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.util.*;

/**
 * 购物车控制器
 *
 * @author fengshuonan
 * @Date 2020-04-26 17:00:50
 */
@Controller
@RequestMapping("/shoppingcart")
public class ShoppingcartController extends BaseController {

    private String PREFIX = "/mall/shoppingcart/";

    @Autowired
    private IShoppingcartService shoppingcartService;

    @Autowired
    private IMerService merService;

    @Autowired
    private IOrderMerService orderMerService;

    @Autowired
    private IOrderService orderService;

    /**
     * 跳转到购物车首页
     */
    @RequestMapping("/shoppingcart_buy")
    public String shoppingcartBuy() {
        return "/mall/shoppingcart_buy.html";
    }

    //将商品添加到购物车
    @RequestMapping("/addcar")
    @ResponseBody
    public Object addcar(Shoppingcart shoppingcart) {
        Integer userId = ShiroKit.getUser().getId();
        shoppingcart.setId(0);
        shoppingcart.setAdddate(new Date());
        shoppingcart.setUserid(userId);
        shoppingcartService.insert(shoppingcart);
        return SUCCESS_TIP;
    }


    /**
     * 删除购物车
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String shoppingcartIds) {
        List<String> list = Arrays.asList(shoppingcartIds.split(","));
        shoppingcartService.deleteBatchIds(list);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到付款界面
     */
    @RequestMapping("/buy")
    public String carBuy(@RequestParam String shoppingcartIds, RedirectAttributes model) {
        Integer userid = ShiroKit.getUser().getId();
        List<String> list = Arrays.asList(shoppingcartIds.split(","));
        List<Shoppingcart> list1 = shoppingcartService.selectBatchIds(list);
        Order order = new Order();
        String orderid = ToolUtil.getRandomString(32);
        BigDecimal sum = new BigDecimal(0.00);
        //将购物车中的每一件商品保存到order_mer表中
        for(Shoppingcart shoppingcart : list1){
            Mer mer = merService.selectById(shoppingcart.getMerid());
            OrderMer orderMer = new OrderMer();
            orderMer.setId(0);
            orderMer.setOrderid(orderid);
            orderMer.setMerid(mer.getId());
            orderMer.setMername(mer.getMername());
            orderMer.setMerattr(shoppingcart.getAttr());
            orderMer.setMerquantity(shoppingcart.getQuantity());
            orderMer.setMerprice(shoppingcart.getPrice());
            orderMer.setMerpictureurl(mer.getPicture());
            orderMerService.insert(orderMer);
            sum = sum.add(shoppingcart.getPrice().multiply(new BigDecimal(shoppingcart.getQuantity())));
        }
        order.setId(orderid);
        order.setSumprice(sum);
        order.setUserid(userid);
        order.setAdddate(new Date());
        order.setState("64");
        orderService.insert(order);
        //删除购物车中的记录
        shoppingcartService.deleteBatchIds(list);
        model.addFlashAttribute("item",order);
        return "redirect:/shoppingcart/shoppingcart_buy" ;
    }

    @RequestMapping("/quickBuy")
    public String quickBuy(Shoppingcart shoppingcart, RedirectAttributes model) {
        Integer userid = ShiroKit.getUser().getId();
        Order order = new Order();
        String orderid = ToolUtil.getRandomString(32);
        BigDecimal sum = new BigDecimal(0.00);
        //将商品保存到order_mer表中
        Mer mer = merService.selectById(shoppingcart.getMerid());
        OrderMer orderMer = new OrderMer();
        orderMer.setId(0);
        orderMer.setOrderid(orderid);
        orderMer.setMerid(mer.getId());
        orderMer.setMername(mer.getMername());
        orderMer.setMerattr(shoppingcart.getAttr());
        orderMer.setMerquantity(shoppingcart.getQuantity());
        orderMer.setMerprice(shoppingcart.getPrice());
        orderMer.setMerpictureurl(mer.getPicture());
        orderMerService.insert(orderMer);
        sum = sum.add(shoppingcart.getPrice().multiply(new BigDecimal(shoppingcart.getQuantity())));
        order.setId(orderid);
        order.setSumprice(sum);
        order.setUserid(userid);
        order.setAdddate(new Date());
        order.setState("64");
        orderService.insert(order);
        model.addFlashAttribute("item",order);
        return "redirect:/shoppingcart/shoppingcart_buy" ;
    }

}
